package com.cc.controller;

import com.cc.model.EbNews;
import com.cc.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private NewsService newsService;


     @RequestMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") int page,
                             @RequestParam(value = "pageSize",defaultValue = "3")int pageSize){
        //1.获取新闻表的第一页数据，每页三条
       List<EbNews> newses = newsService.list(page,pageSize);

        //获取最大页数
        int maxPage = newsService.getMaxPage(pageSize);
        //2.将数据（List）和视图（jsp）返回给中央控制器
        ModelAndView mv = new ModelAndView();
        //绑定数据
        mv.addObject("newses",newses);
        //绑定当前页
        mv.addObject("curPage",page);
        //b绑定最大页
        mv.addObject("maxPage",maxPage);
        //绑定视图
        mv.setViewName("manage/news");
        return mv;
    }

    @RequestMapping("/to_save")
    public String toSave(){
        return "manage/news-add";
    }


    @RequestMapping("/save_commit")
    public String saveCommit(EbNews ebNews){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String timeStr = sdf.format(date);
        ebNews.setEnCreateTime(timeStr);
        if((ebNews.getEnTitle()==null||
                ebNews.getEnTitle().equals("")||
                ebNews.getEnTitle().trim().length()<0)
                ||(ebNews.getEnContent()==null||
                ebNews.getEnContent().equals("")||
                ebNews.getEnContent().trim().length()<0)){
            return "manage/manage-error";
        }
        int result = newsService.save(ebNews);
        if(result<=0){

        }
        return "manage/manage-result";
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id) {
        int result = newsService.deleteByPk(id);
        System.out.println(result);
        if(result==0){

        }
        return "manage/manage-result1";
    }
    @RequestMapping("/edit")
    public String edit(Model model, Integer enId){
        EbNews ebNews = newsService.selectByCondition(enId);
        model.addAttribute("ebNews",ebNews);
        return "manage/news-modify";
    }
    @RequestMapping("/editSubmit")
    public String editSubmit(EbNews ebNews) {
        int result = newsService.updateById(ebNews);
        if(result<=0){
            return "manage/manage-error";

        }
        return "manage/manage-result";
    }

    @RequestMapping("/news-view")
    public String newsView(Model model,int enId){
        EbNews ebNews = newsService.selectByCondition(enId);
        System.out.println(ebNews+"=============ebNew");
        model.addAttribute("ebNews",ebNews);
        return "news-view";
    }
}
